<?php

namespace App\Repositories\Manage;

use App\Models\Dict;

class DictRepository
{
    public function saveOrUpdate($data = [], $id = 0)
    {
        if (intval($id)) {
            Dict::where("id", $id)->update($data);
            return $id;
        } else {
            $res = Dict::create($data);
            return $res['id'] ?? "";
        }
    }

    public function getOne($id = null)
    {
        return Dict::query()->where(['id' => $id])->first();
    }

    /**
     * 批量删除
     * @param null $ids
     */
    public function batchDelete($ids = null)
    {
        if (intval($ids)) {
            Dict::where(['id' => $ids])->delete();
        } else {
            Dict::whereIn("id", $ids)->delete();
        }

        return retSuc([], "删除成功");
    }

    /**
     * 获取类型
     */
    public function getOptions()
    {
        return Dict::query()
            ->where('id', '>', 0)
            ->orderBy("sort", 'asc')
            ->get(['id', 'name', 'code']);
    }
}
